// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 

import java.io.*;
import java.util.*;
import org.apache.log4j.Logger;

// Referenced classes of package com.unlimax.app.kerneos:
//            TMObject, DBObject, CSVObject, MainApp

public class SunSystemsService {

	public SunSystemsService() {
	}

	public void getData(String s, Date date) {
		String s1 = beginDay(s, date);
		String s2 = endDay(s, date);
		TMObject tmobject = new TMObject();
		String s3 = MainApp.bundle.getString("file.path");
		String s4 = MainApp.bundle.getString("file.customer");
		String s5 = MainApp.bundle.getString("file.sales");
		String s6 = MainApp.bundle.getString("obj.bu");
		String as[] = s6.split(",");
		Object obj = null;
		if ((new File(s3)).mkdirs())
			MainApp.logger
					.info((new StringBuilder()).append("Folder ").append(s3)
							.append(" was created successfully.").toString());
		else if ((new File(s3)).isDirectory())
			MainApp.logger.info((new StringBuilder()).append("Folder ")
					.append(s3).append(" has existed.").toString());
		else
			MainApp.logger.error((new StringBuilder()).append("Folder ")
					.append(s3).append(" wasn't created due to a problem.")
					.toString());
		MainApp.logger
				.info("------------------------------------------------------------");
		for (int i = 0; i < as.length; i++) {
			String s7 = as[i].toString();
			DBObject dbobject = new DBObject();
			int j = dbobject.runQuery((new StringBuilder())
					.append("SELECT COUNT(*) FROM ").append(s7)
					.append("_CUST WHERE STATUS = 0").toString());
			try {
				MainApp.logger.info((new StringBuilder()).append(s7)
						.append(" has ").append(j)
						.append(" customer records to be extracted.")
						.toString());
				CSVObject csvobject = new CSVObject();
				StringBuffer stringbuffer = new StringBuffer();
				stringbuffer.append(csvobject.getCUSTHeader());
				stringbuffer.append(csvobject.getCUSTDetails(s7));
				writeCSV(s3, (new StringBuilder()).append(s7).append("_")
						.append(s4).toString(), stringbuffer.toString());
				MainApp.logger.info("Customer extraction:done.");
				MainApp.logger
						.info("------------------------------------------------------------");
			} catch (Exception exception) {
				MainApp.logger.error(exception.getMessage());
			}
			int k = dbobject
					.runQuery((new StringBuilder())
							.append("SELECT COUNT(*) FROM ")
							.append(s7)
							.append("_SIL SIL, ")
							.append(s7)
							.append("_SIL_SOL SILSOL, ")
							.append(s7)
							.append("_SO SO WHERE INV_POSTING_DATETIME BETWEEN '")
							.append(s1)
							.append("' AND '")
							.append(s2)
							.append("' AND SIL.SI_ID = SILSOL.SI_ID AND SIL.SIL_NUM = SILSOL.SIL_NUM AND SILSOL.SO_TXN_REF = SO.SO_TXN_REF AND SO.SALES_DEFN_CODE IN (")
							.append(MainApp.bundle
									.getString((new StringBuilder()).append(s7)
											.append(".type").toString()))
							.append(")").toString());
			try {
				MainApp.logger.info((new StringBuilder()).append(s7)
						.append(" has ").append(k)
						.append(" sales records to be extracted.").toString());
				CSVObject csvobject1 = new CSVObject();
				StringBuffer stringbuffer1 = new StringBuffer();
				stringbuffer1.append(csvobject1.getSIHeader());
				stringbuffer1.append(csvobject1.getSIDetails(s7, s1, s2));
				writeCSV(s3, (new StringBuilder()).append(s7).append("_")
						.append(s5).toString(), stringbuffer1.toString());
				MainApp.logger.info("Sales extraction:done.");
				MainApp.logger
						.info("------------------------------------------------------------");
			} catch (Exception exception1) {
				MainApp.logger.error(exception1.getMessage());
			}
		}

	}

	public String beginDay(String s, Date date) {
		String s1 = null;
		TMObject tmobject = new TMObject();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		if (s.toUpperCase().equals("L")) {
			calendar.set(5, 1);
			calendar.add(2, -1);
		}
		if (s.toUpperCase().equals("C"))
			calendar.set(5, 1);
		if (s.toUpperCase().equals("A")) {
			calendar.add(5, -1);
			calendar.set(6, 1);
		}
		s1 = tmobject.getTransDate("yyyyMMdd", calendar.getTime());
		return s1;
	}

	public String endDay(String s, Date date) {
		String s1 = null;
		TMObject tmobject = new TMObject();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		if (s.equals("L")) {
			calendar.set(5, 1);
			calendar.add(5, -1);
		}
		if (s.equals("C"))
			calendar.set(5, 20);
		if (s.toUpperCase().equals("A"))
			calendar.add(5, -1);
		s1 = tmobject.getTransDate("yyyyMMdd", calendar.getTime());
		return s1;
	}

	private void writeCSV(String s, String s1, String s2) {
		File file = new File(s, s1);
		Object obj = null;
		try {
			OutputStreamWriter outputstreamwriter = new OutputStreamWriter(
					new FileOutputStream(file), "UTF-8");
			outputstreamwriter.write(s2);
			outputstreamwriter.close();
			MainApp.logger.info((new StringBuilder()).append("File:").append(s)
					.append(s1).append(" has been generated.").toString());
		} catch (Exception exception) {
			MainApp.logger.error(exception.getMessage());
		}
	}
}
